const Controller = require("egg").Controller
class UserController extends Controller {
  /**
   * 获取用户列表
   * 分页查询参数 pageSize pageNo 
   */
  async getList(){
    const { ctx } = this,params = ctx.request.body
    try {
      ctx.validate({
        page:{type:"number",require:true,default:1},
        pageSize:{type:"number",require:true,max:30,min:5},
      })
      let data = await this.ctx.service.user.getList(params.page,params.pageSize,params.type)


      const userModel = this.ctx.model.User

      let offset = parseInt((params.page-1)*params.pageSize)


      let roles = ["NORMAL_USER","DQ_SINGER","DQ_OFFICIAL_ACCOUNT"]
      let wh = {}
      if(roles.indexOf(params.type) > -1){ // 如果是合法用户，拼接where条件语句
       wh = {
        type :params.type
       }
      }


      const result = await userModel.findAndCountAll(
          { 
            offset: offset, 
            limit: params.pageSize ,
            where:wh
          }
        )



      ctx.body = { msg:"success",code:0,total:result.count,data:result.rows,};
     
    }catch(err){
      ctx.logger.warn(err.errors);
      ctx.body = { success: false };
      return;
    }
  }
  async getInfo(){
    const { ctx } = this,params = ctx.params
    try {
      ctx.validate({
        id:{type:"string",require:true,default:"0"},
      },params)
      let data = await this.ctx.service.user.getInfo(params.id)
      ctx.body = { msg:"success",code:0, data};
    } catch (err) {
      // console.log(err)
      ctx.logger.warn(err.errors);
      ctx.body = { success: false,msg:"用户不存在",code:500};
      return;
    }
    
  }
}
module.exports = UserController